<template>
  <div class="app-container">
    <el-card>
      <el-breadcrumb
        separator-class="el-icon-arrow-right"
        v-if="subjects.id && subjects.name"
      >
        <el-breadcrumb-item>学科管理</el-breadcrumb-item>
        <el-breadcrumb-item>{{
          subjects.id ? subjects.name : ''
        }}</el-breadcrumb-item>
        <el-breadcrumb-item>目录管理</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 搜索nav栏 -->
      <el-row>
        <el-col :span="20">
          <!-- 搜索框 -->
          <el-col>
            <el-form
              ref="subjectFrom"
              :model="directoryDS"
              label-width="80px"
              inline
            >
              <el-form-item label="目录名称" size="mini">
                <el-input
                  class="inputCol"
                  v-model="directoryDS.directoryName"
                ></el-input>
              </el-form-item>
              <el-form-item label="状态">
                <el-select
                  v-model="directoryDS.state"
                  placeholder="请选择"
                  size="mini"
                >
                  <el-option label="已启用" :value="1"> </el-option>
                  <el-option label="已禁用" :value="0"> </el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-col class="left-fot">
                  <el-button
                    size="mini"
                    class="colInput"
                    plain
                    @click="resetInput"
                    >清除</el-button
                  >
                  <el-button
                    size="mini"
                    class="colInput"
                    type="primary"
                    @click="onSearch"
                    >搜索</el-button
                  >
                </el-col>
              </el-form-item>
            </el-form>
          </el-col>
        </el-col>
        <el-col :span="4">
          <el-button
            class="colright"
            size="mini"
            type="success"
            icon="el-icon-edit"
            @click="addDialogVisible = true"
            >新增学科</el-button
          >
          <el-button
            v-if="subjects.id && subjects.name"
            type="text"
            class="colright"
            icon="el-icon-back"
            @click="$router.back()"
            >返回学科</el-button
          >
        </el-col>
      </el-row>
      <!-- 消息文案 -->
      <el-alert
        :title="`数据一共 ${counts} 条`"
        :closable="false"
        type="info"
        show-icon
      >
      </el-alert>

      <!-- 表格区域 -->
      <el-table :data="directoryList" style="width: 100%">
        <el-table-column type="index" :index="indexMethod" label="序号">
        </el-table-column>
        <el-table-column prop="subjectName" label="所属学科"> </el-table-column>
        <el-table-column prop="directoryName" label="目录名称">
        </el-table-column>
        <el-table-column prop="username" label="创建者"> </el-table-column>
        <el-table-column prop="addDate" label="创建日期"> </el-table-column>
        <el-table-column prop="totals" label="面试数量"> </el-table-column>
        <el-table-column prop="state" label="状态">
          <template v-slot="{ row }">
            {{ row.state === 1 ? '已启用' : '已禁用' }}
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <template v-slot="{ row }">
            <el-button type="text" @click="editState(row)">
              {{ row.state === 1 ? '禁用' : '启用' }}
            </el-button>
            <el-button
              type="text"
              :disabled="row.state === 1"
              @click="editDirectorys(row)"
              >修改</el-button
            >
            <el-button
              type="text"
              :disabled="row.state === 1"
              @click="removeDir(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <directorys-add
        :page="ListData.page"
        :pagesize="ListData.pagesize"
        :counts="counts"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      />
    </el-card>

    <!-- 添加目录弹框 -->
    <el-dialog
      title="新增目录"
      :visible.sync="addDialogVisible"
      width="20%"
      @close="restDir"
    >
      <!-- 主体区域 -->
      <el-row>
        <el-col>
          <el-form
            :model="directorysForm"
            :rules="directorysFormRules"
            ref="directorysFormRef"
            label-width="80px"
            class="ruleForm"
          >
            <el-form-item
              v-if="!subjects.id && !subjects.name"
              class="elform"
              label="所属学科"
            >
              <el-select
                v-model="directorysForm.value"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  :label="item.label"
                  :value="item.value"
                  v-for="item in addFrom"
                  :key="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item class="elform" label="目录名称" prop="directoryName">
              <el-input
                v-model="directorysForm.directoryName"
                placeholder="请输入目录名称"
                style="width: 100%"
              ></el-input>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addDirForm">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改目录弹框 -->
    <el-dialog
      title="修改目录"
      :visible.sync="editDialogVisible"
      width="20%"
      @close="restDir"
    >
      <!-- 主体区域 -->
      <el-row>
        <el-col>
          <el-form
            :model="editList"
            :rules="directorysFormRules"
            ref="directorysFormRef"
            label-width="80px"
            class="ruleForm"
          >
            <el-form-item
              v-if="!subjects.id && !subjects.name"
              class="elform"
              label="所属学科"
            >
              <el-select
                v-model="editList.value"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  :label="item.label"
                  :value="item.value"
                  v-for="item in addFrom"
                  :key="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item class="elform" label="目录名称" prop="directoryName">
              <el-input
                v-model="editList.directoryName"
                placeholder="请输入目录名称"
                style="width: 100%"
              ></el-input>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editDirForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { changeState, remove, update, add, list } from '@/api/hmmm/directorys'
import { simple } from '@/api/hmmm/subjects'
import DirectorysAdd from '../components/directorys-add.vue'
import dayjs from 'dayjs'
export default {
  name: 'Directorys',
  data () {
    return {
      // 数据id'
      infoId: '',
      // 修改请求数据
      editList: {
        directoryName: '',
        value: ''
      },
      // 修改的弹框
      editDialogVisible: false,
      // 禁用状态
      isDisabled: false,
      // 验证
      directorysFormRules: {
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ]
      },
      // 添加目录的数据
      directorysForm: {
        directoryName: '',
        // 下拉框的值
        value: ''
      },
      // 请求的参数
      ListData: {
        page: 1,
        pagesize: 5
      },
      directoryDS: {
        // directoryName: '',
        // state: null
      },
      selectedDirectoryDS: {},
      // 数据列表
      directoryList: [],
      // 总条数
      counts: 0,
      // 添加目录的弹框
      addDialogVisible: false,
      // 简单数据列表
      addFrom: [],
      inputString: ''
    }
  },

  components: {
    DirectorysAdd
  },

  // 调用请求

  created () {
    this.getDirectList()
    this.getAddFrom()
  },
  // 获取地址栏数据
  computed: {
    subjects () {
      return this.$route.query || null
    }
  },

  // 侦听地址栏数据
  watch: {
    '$route.query' () {
      this.getDirectList()
    }
  },
  methods: {
    // 切换状态
    async editState (row) {
      // console.log(row)
      await changeState({
        id: row.id,
        state: row.state === 1 ? 0 : 1
      })
      this.$message.success('操作成功')
      row.state = row.state === 1 ? 0 : 1
    },

    // 删除标签
    async removeDir (row) {
      try {
        await this.$confirm('确定要删除吗', '提示')
        await remove(row)
        // console.log(data)
        if (this.directoryList.length === 1 && this.ListData.page > 1) {
          this.ListData.page--
        }
        this.$message.success('删除成功!')
        this.getDirectList()
      } catch (e) {
        // this.$message.error('删除失败!')
      }
    },

    // 获取学科简单列表
    async getAddFrom () {
      const { data } = await simple()
      // console.log(data)
      this.addFrom = data
    },

    // 修改
    async editDirForm () {
      if (this.subjects.id && this.subjects.name) {
        await update({
          id: this.infoId,
          subjectID: +this.subjects.id,
          directoryName: this.editList.directoryName
        })
        this.$message.success('修改目录成功')
        this.editDialogVisible = false
        this.getDirectList()
      } else {
        await update({
          id: this.infoId,
          subjectID: this.editList.value,
          directoryName: this.editList.directoryName
        })
        this.$message.success('修改目录成功')
        this.editDialogVisible = false
        this.getDirectList()
      }
    },

    // 点击修改显示弹框
    editDirectorys (row) {
      this.editList.value = row.subjectID
      // console.log(row.subjectID)
      // console.log(row)
      this.infoId = row.id
      this.editList.directoryName = row.directoryName
      this.editDialogVisible = true
    },

    // 添加
    async addDirForm () {
      if (this.subjects.id && this.subjects.name) {
        await add({
          subjectID: +this.subjects.id,
          directoryName: this.directorysForm.directoryName
        })
        this.$message.success('新增目录成功')
        this.addDialogVisible = false
        this.getDirectList()
      } else {
        await add({
          subjectID: this.directorysForm.value,
          directoryName: this.directorysForm.directoryName
        })
        this.$message.success('新增目录成功')
        this.addDialogVisible = false
        this.getDirectList()
      }
    },

    // 重置表单
    restDir () {
      this.$refs.directorysFormRef.resetFields()
      this.directorysForm.value = ''
    },
    // 清除数据
    resetInput () {
      this.selectedDirectoryDS = {}
      this.directoryDS = {}
      this.getDirectList()
      // this.getDirectList()
    },

    // 请求目录列表
    async getDirectList () {
      if (this.subjects.id && this.subjects.name) {
        const { data } = await list({ ...this.selectedDirectoryDS, subjectID: +this.subjects.id, ...this.ListData })
        // console.log(data)
        this.directoryList = data.items
        this.counts = data.counts
        data.items.forEach((item) => {
          item.addDate = dayjs(item.addDate).format('YYYY-MM-DD hh:mm:ss')
        })
      } else {
        const { data } = await list({ ...this.selectedDirectoryDS, ...this.ListData })
        // console.log(data)
        this.ListData.subjectID = this.subjects.id || null
        data.items.forEach((item) => {
          item.addDate = dayjs(item.addDate).format('YYYY-MM-DD hh:mm:ss')
        })
        this.directoryList = data.items
        this.counts = data.counts
      }
    },

    // 一页多少条
    async handleSizeChange (newSize) {
      this.ListData.pagesize = newSize
      this.ListData.page = 1
      this.getDirectList()
    },

    // 多少页
    async handleCurrentChange (newPage) {
      this.ListData.page = newPage

      this.getDirectList()
    },
    // 搜索
    onSearch () {
      this.ListData.page = 1
      this.selectedDirectoryDS = { ...this.directoryDS }

      // this.ListData = {
      //   ...this.ListData,
      //   ...this.directoryDS
      // }
      this.getDirectList()
    },
    indexMethod (index) {
      return (this.ListData.page - 1) * this.ListData.pagesize + index + 1
    }
  }
}
</script>

<style scoped lang="scss">
.left-fot {
  margin-left: 9px;
}
.el-row {
  margin-top: 20px;
}
.colInput {
  height: 35px;
}
.colright {
  float: right;
  height: 35px;
  margin-left: 10px;
}
.el-breadcrumb {
  padding-bottom: 20px;
  border-bottom: 1px solid #ccc;
}
.inputCol {
  width: 200px;
}
.el-form-item {
  display: inline-block;
}
.el-table {
  margin-top: 10px;
}
.el-pagination {
  float: right;
  margin-top: 20px;
}
.elform {
  width: 100%;
}

.el-form-item__content {
  margin-left: 10px;
}
</style>
